package org.spring.boot.sample.hystrix.command.http;

import java.io.InputStream;
import java.net.URL;

import org.apache.tomcat.util.http.fileupload.IOUtils;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.hystrix.HystrixThreadPoolProperties;

public class SmsSendCommand extends HystrixCommand<String> {

	public SmsSendCommand() {
		super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("SmsGroup"))  
                .andCommandKey(HystrixCommandKey.Factory.asKey("SmsCommandKey"))
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("SmsThreadPool"))
                .andCommandPropertiesDefaults(
                	HystrixCommandProperties.Setter()
                		.withExecutionTimeoutInMilliseconds(10000)
                )
                .andThreadPoolPropertiesDefaults(
                	HystrixThreadPoolProperties.Setter()
                		.withCoreSize(40)	// 配置线程池里的线程数
                		.withMaxQueueSize(50)
                		.withQueueSizeRejectionThreshold(40)
                )
        );
	}

	@Override
	protected String run() throws Exception {
		URL url = new URL("http://127.0.0.1:8000/sms/15823345333");
		byte[] result = new byte[8];
		InputStream input = url.openStream();
		IOUtils.readFully(input, result);
		String s = new String(result);
		return s;
	}

	@Override
	protected String getFallback() {
		return "报错";
	}

}
